<template>
  <div class="register-container">
    <div class="register-card">
      <div class="card-title">
        <h2>创建新账号</h2>
        <p>加入糯米团子の博客</p>
      </div>
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-width="0px">
        <el-form-item prop="account">
          <el-input 
            v-model="registerForm.account" a
            placeholder="手机号"
            prefix-icon="el-icon-phone"
          ></el-input>
        </el-form-item>
        <el-form-item prop="nickname">
          <el-input 
            v-model="registerForm.nickname" 
            placeholder="昵称"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="确认密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email" 
            placeholder="邮箱"
            prefix-icon="el-icon-message"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading"
            @click="handleRegister"
            class="register-button"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div class="register-option">
        <router-link to="/login" class="option-link">已有账号? 立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      registerForm: {
        account: '',
        nickname: '',
        password: '',
        confirmPassword: '',
        email: ''
      },
      registerRules: {
        account: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 16, message: '昵称长度在2-16个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },
    handleRegister() {
      this.$refs.registerFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$axios.post('/user/register', this.registerForm)
            .then(res => {
              if (res.data.code === 200) {
                console.log(res.data)
                console.log(res.data.data)
                this.$message.success('注册成功，请登录')
                this.$router.push('/login')
              } else {
                this.$message.error(res.data.message)
              }
            })
            .catch(err => {
              console.error(err)
              this.$message.error('注册失败，请稍后重试')
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 180px);
  padding: 20px;
}

.register-card {
  background-color: white;
  border-radius: 20px;
  box-shadow: -10px -10px 20px rgba(255, 255, 255, 0.8), 
              10px 10px 20px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  position: relative;
  overflow: hidden;
}

.register-card::before {
  content: '';
  position: absolute;
  bottom: -50px;
  left: -50px;
  width: 100px;
  height: 100px;
  background-color: rgba(255, 183, 107, 0.2);
  border-radius: 50%;
}

.card-title {
  margin-bottom: 30px;
}

.card-title h2 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
}

.card-title p {
  color: #909399;
}

.register-button {
  width: 100%;
  border-radius: 20px;
  background-color: #ffb76b;
  border-color: #ffb76b;
  height: 40px;
  transition: all 0.3s;
}

.register-button:hover {
  background-color: #ff9d34;
  border-color: #ff9d34;
  transform: translateY(-2px);
  box-shadow: 0 5px 10px rgba(255, 157, 52, 0.2);
}

.register-option {
  margin-top: 20px;
  text-align: center;
}

.option-link {
  color: #909399;
  text-decoration: none;
  transition: color 0.3s;
}

.option-link:hover {
  color: #ffb76b;
}
</style>    